package System.Task;

import patterns.chain.CollisionHandler;
import patterns.chain.EmergencyHandler;
import patterns.chain.LowFuelHandler;
import patterns.chain.SystemFailureHandler;
import patterns.command.MachineCommand;
import patterns.command.TaskPriority;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskManager {
    private Map<String, Task> tasks = new HashMap<>();
    private List<MachineCommand> commandHistory = new ArrayList<>();
    private EmergencyHandler emergencyHandler;

    public TaskManager() {
        // 初始化责任链
        emergencyHandler = new LowFuelHandler();
        EmergencyHandler collisionHandler = new CollisionHandler();
        EmergencyHandler systemFailureHandler = new SystemFailureHandler();

        emergencyHandler.setNextHandler(collisionHandler);
        collisionHandler.setNextHandler(systemFailureHandler);
    }

    // 任务管理方法
    public Task createTask(String description, String machineId, String fieldId, TaskPriority priority) {
        String taskId = "T" + System.currentTimeMillis();
        Task task = new Task(taskId, description, machineId, fieldId, priority);
        tasks.put(taskId, task);
        System.out.println("创建新任务: " + taskId);
        return task;
    }

    public Task getTask(String taskId) {
        return tasks.get(taskId);
    }

    public List<Task> getAllTasks() {
        return new ArrayList<>(tasks.values());
    }

    // 命令执行方法
    public void executeCommand(MachineCommand command) {
        command.execute();
        commandHistory.add(command);
    }

    public void undoLastCommand() {
        if (commandHistory.isEmpty()) {
            System.out.println("没有可撤销的命令");
            return;
        }

        MachineCommand lastCommand = commandHistory.remove(commandHistory.size() - 1);
        lastCommand.undo();
    }

    // 紧急情况处理
    public void handleEmergency(String emergencyType, String taskId, String details) {
        Task task = tasks.get(taskId);
        if (task != null) {
            emergencyHandler.handleRequest(emergencyType, task, details);
        } else {
            System.out.println("警告: 未找到任务 " + taskId + ", 无法处理紧急情况");
        }
    }

    // 显示任务列表
    public void displayTaskList() {
        System.out.println("\n=== 任务列表 ===");
        System.out.printf("%-10s%-15s%-12s%-10s%-15s%-20s%n",
                "任务ID", "农机ID", "优先级", "状态", "创建时间", "描述");

        for (Task task : tasks.values()) {
            System.out.printf("%-10s%-15s%-12s%-10s%-15s%-20s%n",
                    task.getTaskId(),
                    task.getMachineId(),
                    task.getPriority().getDescription(),
                    task.getStatus().getDescription(),
                    task.getCreateTime().format(DateTimeFormatter.ofPattern("HH:mm:ss")),
                    task.getDescription().length() > 18 ?
                            task.getDescription().substring(0, 15) + "..." :
                            task.getDescription());
        }
    }
}
